let app = getApp();
Page({
    data: {
       city:"",
       province:'',
       header:{},
       userInfo:{}, 
       title:'',
       content:'',
       topic:[{"id":10,"name":"求职","type":10,"sort":2},{"id":11,"name":"招聘","type":20,"sort":3},{"id":12,"name":"求购","type":30,"sort":4},{"id":17,"name":"租赁","type":80,"sort":9},{"id":19,"name":"办证","type":100,"sort":11}],
       hotTopicType:null,
       changeId:null

   },
   turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=6'
      })
    },
    onShow: function() {
     
       var chooseProvince= wx.getStorageSync('chooseProvince6');
       var chooseCity2=wx.getStorageSync('chooseCity6');
        if (chooseProvince) {
          this.setData({
            city:wx.getStorageSync('chooseCity6')||'全国',
            item:wx.getStorageSync('chooseCity6')||'全国',
            province:wx.getStorageSync('chooseProvince6')||'全国'
          })
        }
    

    },
   onLoad: function(options) {
    this.setData({
      changeId:options.id
    })
    getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header
       })  
       this.gethotTopic();
     })
     
   },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
   
     //获取热门话题
    gethotTopic:function(){
        var _self=this;
       //   wx.request({
       //      url: getApp().globalData.baseUrl + '/wx/articles/hotTopic',
       //      method:"GET",
       //      header: this.data.header,
       //      success: function(res) {
       //       console.log(res.data)
       //      if (res.data.code==200) {
       //       for(var i=0;i<res.data.obj.length;i++){
       //          res.data.obj[i].select='nomal';
       //       }
       //       res.data.obj[0].select='select';
       //        _self.setData({
       //          topic:res.data.obj,
       //          hotTopicType:res.data.obj[0].type
       //         })  
       //        _self.getBbsDetail();     
       //       }
           
       //   }
       // })
       this.getBbsDetail(); 
    },
    //切换热门话题
    getBbs:function(e){
       var id=e.target.dataset.code;
       var arr=this.data.topic;
       var name='';
       for(var j = 0, length2 = arr.length; j < length2; j++){
           if (id==arr[j].type) {
            arr[j].select='select';
            name=arr[j].name;
           }else{
            arr[j].select='nomal';
           }
           
       }
       console.log(123)
       this.setData({
          topic:arr,
          hotTopicType:id
       }) 
    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏

    },
    getBbsDetail:function () {
       var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/detail?id='+this.data.changeId,
            method:"GET",
            header: this.data.header,
            success: function(res) {
             console.log(res.data.obj)
             var obj=res.data.obj;
            
             var arr=_self.data.topic;
           
             for(var j = 0, length2 = arr.length; j < length2; j++){
                 if (obj.hotTopicType==arr[j].type) {
                  arr[j].select='select';
                
                 }else{
                  arr[j].select='nomal';
                 }
                 
             }
           wx.setStorageSync('chooseProvince6',obj.province);
            wx.setStorageSync('chooseCity6',obj.city);
              _self.setData({
              title:obj.title,
              content:obj.content,
              hotTopicType:obj.hotTopicType,
              topic:arr,
              city:obj.city,
              province:obj.province,
              linshiArr:obj.wxArticlesPic
             })
             
         }
       })
    },

    textTitle:function (e) {
      console.log(e.detail.value)
       this.setData({
         title:e.detail.value
       })
    },
    textContent:function (e) {
       console.log(e.detail.value)
       this.setData({
         content:e.detail.value
       })
    },
    onMyEvent:function(province){
      console.log(province.detail)
      this.setData({
        province:province.detail.split(',')[0],
        city:province.detail.split(',')[1]
      })
      
     
    },
    sendSheQu: function(){
        console.log(this.data.title)
        // return;
        if (this.data.linshiArr.length>6) {
             wx.showToast({
              title: '最多上传6张图片',
              icon:'none',
              duration: 2000
            })
             var  yarr=this.data.linshiArr.slice(0,6);

             this.setData({
               linshiArr:yarr
             })
      
       }
        wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/update',
            method:"POST",
            data:{
                'id':this.data.changeId,
                'openid':this.data.userInfo.openid,
                "hotTopicType": this.data.hotTopicType,//主题
                "title": this.data.title,//标题
                "content": this.data.content,//内容
                "province": this.data.province,
                "city": this.data.city,
                "area": "",
                "latitude": "",
                "longitude": "",
                'status':10,
                "wxArticlesPic":this.data.linshiArr
            },
            header: this.data.header,
            success: function(res) {
               console.log(res)
                  wx.navigateTo({
                 url: '/pages/mineTieZi/mineTieZi'
               })
                                    
            }
            })

    },chooseImage:function () {
      var _self=this;
      wx.chooseImage({
        success (res) {
          const tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths[0])
          wx.uploadFile({
            url:getApp().globalData.baseUrl+'/wx/img/url', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            header: _self.data.header,
            name: 'filePath',
            formData: {
             // openid:_self.data.userInfo.openid 
            },
            success (res){
               var data = JSON.parse(res.data);
             console.log(data.obj);
              var arr=_self.data.linshiArr;
              var json={url:data.obj}
              arr.push(json)
              
                _self.setData({
                    linshiArr:arr
                })
            }
          })
        }
      })
    },
     deleteImg:function (e) {
       console.log(e.target.dataset.code);
       var index=e.target.dataset.code;
       var arr=this.data.linshiArr;
       arr.splice(index,1);
       this.setData({
        linshiArr:arr
       })
    }


})